package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.EmpInfoRequireType;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.CompanyAllEmpInfoRequireUpdateDTO;
import org.jsola.hr.dto.CompanyEmpInfoRequireAddDTO;
import org.jsola.hr.dto.CompanyEmpInfoRequireUpdateDTO;
import org.jsola.hr.query.CompanyEmpInfoRequireQuery;
import org.jsola.hr.service.ICompanyEmpInfoRequireService;
import org.jsola.hr.vo.CompanyEmpAllInfoRequireVO;
import org.jsola.hr.vo.CompanyEmpInfoRequireListVO;
import org.jsola.hr.vo.CompanyEmpInfoRequireVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * @author zhr
 */
@RestController("hrCompanyEmpInfoRequireController")
@RequestMapping(HrConstants.API_PATH + "/v1/companyEmpInfoRequires")
public class CompanyEmpInfoRequireController implements ICompanyEmpInfoRequireControllerAPI {

    @Autowired
    private ICompanyEmpInfoRequireService companyEmpInfoRequireService;

    @Override
    @PostMapping
    public Result<CompanyEmpInfoRequireVO> save(@Valid @RequestBody CompanyEmpInfoRequireAddDTO companyEmpInfoRequireAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增公司员工所需信息权限校验

        return Result.success(
                companyEmpInfoRequireService.save(companyEmpInfoRequireAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody CompanyEmpInfoRequireUpdateDTO companyEmpInfoRequireUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改公司员工所需信息权限校验

        int count = companyEmpInfoRequireService.update(companyEmpInfoRequireUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }




    @Override
    @GetMapping(value = "/{companyEmpInfoRequireId}")
    public Result<CompanyEmpInfoRequireVO> findById(@PathVariable String companyEmpInfoRequireId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看公司员工所需信息权限校验

        return Result.success(
            companyEmpInfoRequireService.selectById(companyEmpInfoRequireId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<CompanyEmpInfoRequireListVO>> listAll(@Valid CompanyEmpInfoRequireQuery companyEmpInfoRequireQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看公司员工所需信息权限校验

        return Result.success(
            companyEmpInfoRequireService.select(companyEmpInfoRequireQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<CompanyEmpInfoRequireListVO>> page(@Valid CompanyEmpInfoRequireQuery companyEmpInfoRequireQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            companyEmpInfoRequireService.selectPage(companyEmpInfoRequireQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/byCompanyId/{companyId}/{type}")
    public Result<CompanyEmpAllInfoRequireVO> findByCompanyId(@PathVariable String companyId,
                                                              @PathVariable String type) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看公司员工所需信息权限校验

        return Result.success(
                companyEmpInfoRequireService.findByCompanyId(companyId, type, tokenUser)
        );
    }

    @Override
    @PutMapping(value = "/updateRequireById/{companyEmpInfoRequireId}/{code}/{value}")
    public Result<Integer> updateInfoRequireById(@PathVariable String companyEmpInfoRequireId,
                                                 @PathVariable String code,
                                                 @PathVariable Integer value) {
        if (value == null || EmpInfoRequireType.find(value) == null) {
            return Result.fail("员工信息要修改的状态不正确");
        }
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看公司员工所需信息权限校验

        return Result.success(
                companyEmpInfoRequireService.updateInfoRequireById(companyEmpInfoRequireId, code, value, tokenUser.getSiteId())
        );
    }

    @Override
    @PutMapping(value = "/updateRequireByCompany/{companyId}/{type}/{code}/{value}")
    public Result<Integer> updateInfoRequireByCompanyId(@PathVariable String companyId,
                                                        @PathVariable String type,
                                                        @PathVariable String code,
                                                        @PathVariable Integer value) {
        if (value == null || EmpInfoRequireType.find(value) == null) {
            return Result.fail("员工信息要修改的状态不正确");
        }
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看公司员工所需信息权限校验

        return Result.success(
                companyEmpInfoRequireService.updateInfoRequireByCompanyId(companyId, type, code, value, tokenUser.getSiteId())
        );
    }

    @Override
    @PutMapping(value = "/updateRalation")
    public Result<Integer> updateRalation(@Valid @RequestBody CompanyAllEmpInfoRequireUpdateDTO requireUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改公司员工所需信息权限校验

        int count = companyEmpInfoRequireService.updateRalation(requireUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }


}

